/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.exception.BizException;
import me.zhengjie.modules.biz.domain.Profession;
import me.zhengjie.modules.biz.domain.req.ZsbProfessionReq;
import me.zhengjie.modules.biz.domain.resp.ProfessionQryResp;
import me.zhengjie.modules.biz.mapper.ProfessionDictMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.biz.service.ProfessionService;
import me.zhengjie.modules.biz.domain.vo.ProfessionQueryCriteria;
import me.zhengjie.modules.biz.mapper.ProfessionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;

import me.zhengjie.utils.PageResult;

import static me.zhengjie.modules.biz.domain.enums.ErrorCode.BODY_NOTNULL;

/**
* @description 服务实现
* @author yiwei.he
* @date 2024-03-11
**/
@Service
@RequiredArgsConstructor
public class ProfessionServiceImpl extends ServiceImpl<ProfessionMapper, Profession> implements ProfessionService {

    private final ProfessionMapper professionMapper;
    private final ProfessionDictMapper professionDictMapper;
    @Override
    public PageResult<Profession> queryAll(ProfessionQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(professionMapper.findAll(criteria, page));
    }

    @Override
    public List<Profession> queryAll(ProfessionQueryCriteria criteria){
        return professionMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Profession resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Profession resources) {
        Profession profession = getById(resources.getId());
        profession.copy(resources);
        saveOrUpdate(profession);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Profession> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Profession profession : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("院校名称", profession.getUname());
            map.put("专业", profession.getProfession());
            map.put("删除【0：未删除，1已删除】", profession.getIsDelete());
            map.put("创建时间", profession.getCreateTime());
            map.put("修改时间", profession.getUpdateTime());
            map.put("专业类型【1：本科 2：专科】", profession.getProfessionType());
            map.put("查询类型【1：统招 2：专升本 3：专科】", profession.getCategory());
            map.put("年份", profession.getYears());
            map.put("院校代码", profession.getUcode());
            map.put("批次【0：本科提前批 1：本科一批次 2：本科二批次】", profession.getBatch());
            map.put("科类【1：文科 2：理科】", profession.getSubjectType());
            map.put("性别", profession.getGender());
            map.put("录取人数", profession.getEnrollNum());
            map.put("最高分", profession.getMaxScore());
            map.put("平均分", profession.getAvgScore());
            map.put("最低分", profession.getMinScore());
            map.put("最低位次", profession.getMinSort());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 通过专业查看有那些院校可供选择
     * @param professionList
     * @return
     */
    @Override
    public List<String> professionList(List<String> professionList) {
        return professionMapper.professionList(professionList);
    }

    @Override
    public ProfessionQryResp professionQry(ProfessionQueryCriteria criteria) throws BizException {
        ProfessionQryResp resp = new ProfessionQryResp();
        if(null == criteria){
            throw new BizException(BODY_NOTNULL.getCode(),BODY_NOTNULL.getMessage());
        }
        // 统招需要将支持专升本的专业和单招的专业都查询出来
        if(1 == criteria.getCategory()){
            criteria.setCategory(null);
            return professionQry(criteria, resp);
        }
        return professionQry(criteria, resp);
    }
    @Override
    public PageResult<Profession> getListByType(ProfessionQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(professionMapper.getListByType(criteria, page));
    }

    @Override
    public PageResult<Profession> zsbList(ZsbProfessionReq req, Page<Object> page) {
        ProfessionQueryCriteria criteria = new ProfessionQueryCriteria();
        criteria.setUname(req.getUname());
        criteria.setProfession(req.getProfession());
        return  PageUtil.toPage(professionMapper.findAll(criteria, page));
    }

    private ProfessionQryResp professionQry(ProfessionQueryCriteria criteria, ProfessionQryResp resp) {
        List<Profession> professionList = professionMapper.findAll(criteria);
        List<Profession> universitiesProfessionList =
                professionList.stream().filter(profession -> profession.getProfessionType() == 0).collect(Collectors.toList());
        List<Profession> collegeProfessionList =
                professionList.stream().filter(profession -> profession.getProfessionType() == 1).collect(Collectors.toList());
        resp.setCollegeProfessionList(collegeProfessionList);
        resp.setUniversitiesProfessionList(universitiesProfessionList);
        return resp;
    }
}